package com.podmanager.RssFeed;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


/*
 * Separates the image path and description from the html <img> tag embedded in the xml <description> tag
 */
public class DescriptionParser {
	private String descriptionTag;
	private String description;
	private String imageUrl;
	
	public static final String testTag = "<description>" +
			"<img src=http://imgs.xkcd.com/comics/frequentists_vs_bayesians.png title='Detector! " +
			"What would the Bayesian statistician say if I asked him whether the--' [roll] " +
			"'I AM A NEUTRINO DETECTOR, NOT A LABYRINTH GUARD. SERIOUSLY, DID YOUR BRAIN FALL OUT?' [roll] " +
			"'... yes.' alt='Detector! What would the Bayesian statistician say if I asked him whether the--' " +
			"[roll] 'I AM A NEUTRINO DETECTOR, NOT A LABYRINTH GUARD. SERIOUSLY, DID YOUR BRAIN FALL OUT?' [roll] " +
			"'... yes.' /></description>";
	
	public DescriptionParser() { }
	
	public DescriptionParser(String descriptionTag) {
		super();
		this.descriptionTag = descriptionTag;
	}

	public String getDescriptionTag() {
		return descriptionTag;
	}

	public void setDescriptionTag(String descriptionTag) {
		this.descriptionTag = descriptionTag;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	
	/*
	 * Extracts the src and title attributes and values from an <img> tag
	 */
	public void splitImageTag() {
		Document doc = Jsoup.parse(this.descriptionTag);
		Element link = doc.select("img").first();
		this.imageUrl = link.attr("src");
		this.description = link.attr("title");
	}
	
	
	/*
	 * Returns attribute values given a section of html (content), tag name, and attributes.
	 */
	public static String[] getTagAttributes(String content, String tagName, String[] attributes) {
		String[] attributeValues = new String[attributes.length];
		Document doc = Jsoup.parse(content);
		Element element = doc.select(tagName).first();
		
		for(int i=0; i <= attributes.length - 1; i++) 
			attributeValues[i] = element.attr(attributes[i]);
		
		return attributeValues;
	}	
	
	/*
	 * Returns attribute values given:
	 *  a section of html (content), tag name, attributes, and position.
	 */
	public static String[] getTagAttributesByIndex(String content, String tagName, String[] attributes, int position) {
		String[] attributeValues = new String[attributes.length];
		Document doc = Jsoup.parse(content);
		
		try {
			Element element = doc.select(tagName).get(position);
			
			for(int i=0; i <= attributes.length - 1; i++) 
				attributeValues[i] = element.attr(attributes[i]);
		} catch (Exception e) {
			return null;
		}
		
		return attributeValues;
	}
	
	/*
	 * Returns attribute values given a section of html (content), tag name, and attributes,
	 * but the input attributes must contain a hashmap containing the tag names as keys and the values
	 * must be null
	 */
	public static Map<String, String> getTagAttributes(String content, String tagName, Map<String, String> attributes) {
		Document doc = Jsoup.parse(content);
		Element element = doc.select(tagName).first();
		
		Set<Entry<String, String>> s = attributes.entrySet();
		Iterator<Entry<String, String>> it = s.iterator();
		
		while (it.hasNext()) {
			Entry<String, String> m = (Entry<String, String>)it.next();
			String key = (String) m.getKey();
			String attributeValue = element.attr(key);
			m.setValue((String) attributeValue);
		}
		
		return attributes;
	}
	


	/*
	 * Prints hashmap items to console.
	 */
    public static void retrieveValuesFromListMethod(Map<String, String> map)
    {
        Set<String> keys = map.keySet();
        Iterator<String> itr = keys.iterator();
 
        String key;
        String value;
        while(itr.hasNext())
        {
            key = (String)itr.next();
            value = (String)map.get(key);
            System.out.println(key + " - "+ value);
        }
    }
    
	@Override
	public String toString() {
		return "DescriptionParser [" + "description=" + description + ", imageUrl=" + imageUrl
				+ "]";
	}
	
	
}







